package cc.fs.kotlin.ui.activity.helper

import android.app.Fragment
import android.os.Bundle
import android.support.v7.widget.Toolbar
import android.widget.FrameLayout
import cc.fs.kotlin.R
import cc.fs.kotlin.utils.bindView

/**
 * Created by fostion on 2/19/16.
 */
abstract class SingleFragmentActivity : BaseActivity() {

    val toolbar: Toolbar by bindView(R.id.toolbar)
    val contain: FrameLayout by bindView(R.id.fragment_contain)
    var fragment: Fragment ? = null

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        this.setContentView(R.layout.activity_single_base)

        setUpFragment()
        afterCreate()
    }

    /** fragment初始化 */
    private fun setUpFragment() {
        val fragmentTransaction = fragmentManager.beginTransaction()
        fragment = fragmentManager.findFragmentByTag(fragmentClass.name)
        if (fragment?.isDetached ?: true) {
            fragment = Fragment.instantiate(this, fragmentClass.name, fragmentBundle.invoke())
            fragmentTransaction.add(R.id.fragment_contain, fragment, fragmentClass.name)
        }
        fragmentTransaction.commit()
    }

    abstract fun afterCreate()
    abstract val fragmentClass: Class<*>
    abstract val fragmentBundle: (() -> Bundle?)
}